<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="购物车id" prop="cartId">
      <el-input v-model="dataForm.cartId" placeholder="购物车id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="count">
      <el-input v-model="dataForm.count" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="购物总价" prop="itemTotalPrice">
      <el-input v-model="dataForm.itemTotalPrice" placeholder="购物总价"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="修改日期" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="修改日期"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          cartId: '',
          memberId: '',
          count: '',
          productId: '',
          productName: '',
          itemTotalPrice: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          cartId: [
            { required: true, message: '购物车id不能为空', trigger: 'blur' }
          ],
          memberId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          itemTotalPrice: [
            { required: true, message: '购物总价不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '修改日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cloudgallery/cartitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cartId = data.cartItem.cartId
                this.dataForm.memberId = data.cartItem.memberId
                this.dataForm.count = data.cartItem.count
                this.dataForm.productId = data.cartItem.productId
                this.dataForm.productName = data.cartItem.productName
                this.dataForm.itemTotalPrice = data.cartItem.itemTotalPrice
                this.dataForm.gmtCreate = data.cartItem.gmtCreate
                this.dataForm.gmtModified = data.cartItem.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cloudgallery/cartitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'cartId': this.dataForm.cartId,
                'memberId': this.dataForm.memberId,
                'count': this.dataForm.count,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'itemTotalPrice': this.dataForm.itemTotalPrice,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
